<style>
    .companyFormPhotos {
        /* margin-left:140px; */
        line-height: 38px;
        float: left;
    }

    .companyFormPhotos img {
        cursor: pointer;
        height: 38px;
        width: auto;
        max-width: 200px;
    }

    .warehouseOrderContractForm .layui-form-label {
        width: 100px;
    }

    .warehouseOrderContractForm .layui-input-block {
        margin-left: 130px;
    }

</style>
<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <!-- 头部操作栏 -->
            <div class="layui-form lay-header">
                <div class="toolbar">
                    <div class="layui-form-item lay-query-content">
                        <div class="lay-query-box">
                            <div class="query-lt">
                                <div class="layui-inline">
                                    <label class="layui-form-label w-auto">订单编号</label>
                                    <div class="layui-input-inline mr0">
                                        <input
                                                name="settledOrder"
                                                class="layui-input"
                                                type="text"
                                                placeholder="请输入订单编号"
                                                autocomplete="off"
                                        />
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <button
                                            class="layui-btn icon-btn color-green"
                                            lay-filter="formSubSearchLog"
                                            lay-submit
                                            layui-form-keyDownSearch="true"
                                    >
                                        查询
                                    </button>
                                    <button
                                            class="layui-btn icon-btn color-white"
                                            lay-filter="resets"
                                            lay-submit
                                    >
                                        重置
                                    </button>
                                </div>
                            </div>
                            <div class="query-rt"></div>
                        </div>
                    </div>
                </div>
                <div class="lay-btn-rows">
                    <div class="lay-btn-box">
                        <div class="lay-btn-lt">
                            <button
                                    id="warehouseOrderBtnAdd"
                                    class="layui-btn icon-btn color-reseda"
                            >
                                添加
                            </button>
                        </div>
                        <div class="lay-btn-rt"></div>
                    </div>
                </div>
            </div>
            <!-- 表格 -->
            <table
                    class="layui-table"
                    id="warehouseOrderTable"
                    lay-filter="warehouseOrderTable"
            ></table>
        </div>
    </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="warehouseOrderTableBar">
    {{#  if(d.contractCode !=  ''){}}
    <a class="layui-btn layui-btn-warm layui-btn-xs" lay-event="info">详情</a>
    {{# }}}
    {{#  if(d.contractCode ==  ''){}}
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="process">审核</a>
    {{# }}}
    {{#  if(d.status !=  2 && d.status !=  3&&d.status !=  1){}}
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="withdraw">撤销</a>
    {{# }}}
</script>

<!-- 添加表单弹窗 -->
<script type="text/html" id="warehouseOrderContractForm">
    <form lay-filter="warehouseOrderContractForm" class="layui-form warehouseOrderContractForm model-form">
        <div class="layui-form-item">
            <label class="layui-form-label">仓库编号</label>
            <div class="layui-input-block">
                <select
                        id="factoryId"
                        name="factoryId"
                        lay-filter="factoryId"
                        lay-verify="required"
                        lay-search
                        required
                ></select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">客户编码</label>
            <div class="layui-input-block">
                <select
                        id="trusteeshipcontractId"
                        name="trusteeshipcontractId"
                        lay-filter="trusteeshipcontractId"
                        lay-verify="required"
                        lay-search
                        required
                ></select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">客户级别</label>
            <div class="layui-input-block">
                <input id="customerLevelId" name="customerLevelId"  disabled type="text" placeholder="普通客户" class="layui-input"/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">联系人</label>
            <div class="layui-input-block">
                <select
                        id="contactId"
                        name="contactId"
                        lay-filter="contactId"
                        lay-verify="required"
                ></select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">电话</label>
            <div class="layui-input-block">
                <input id="phone" name="phone" placeholder="请填写电话" type="text" class="layui-input"/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">邮箱</label>
            <div class="layui-input-block">
                <input id="mail" name="mail" placeholder="请输入邮箱" type="text" class="layui-input"/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">申请日期</label>
            <div class="layui-input-block">
                <input
                        type="text"
                        class="layui-input"
                        id="contractTime"
                        name="contractTime"
                        placeholder="请选择申请日期"
                        readonly
                />
            </div>
        </div>


        <!--                <div class="layui-col-xs3">-->
        <!--                    <label class="layui-form-label">邮箱</label>-->
        <!--                    <div class="layui-input-block">-->
        <!--                        <input id="mail" name="mail" type="text" class="layui-input" />-->
        <!--                    </div>-->
        <!--                </div>-->

<!--        <div class="layui-form-item">-->
<!--            <label class="layui-form-label">有效截止日期</label>-->
<!--            <div class="layui-input-block">-->
<!--                <input-->
<!--                        type="text"-->
<!--                        class="layui-input"-->
<!--                        id="storeTime"-->
<!--                        name="storeTime"-->
<!--                        placeholder="请选择有效截止日期"-->
<!--                        readonly-->
<!--                />-->
<!--            </div>-->
<!--        </div>-->
        <div class="layui-form-item">
            <label class="layui-form-label">备注</label>
            <div class="layui-input-block">
                <input id="remark" name="remark" placeholder="请填写备注" type="text" class="layui-input"/>
            </div>
        </div>
        <div class="layui-form-item text-right">
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
            <button class="layui-btn" lay-filter="warehouseOrderContractFormSubmit" type="button" lay-submit>提交</button>
        </div>
    </form>
</script>

<!--首页表单操作项-->
<script type="text/html" id="warehouseProductFormTableBar">
    <button
            class="layui-btn layui-btn-danger layui-btn-xs"
            lay-event="del"
            type="button"
    >
        删除
    </button>
</script>

<!-- js部分 -->
<script>
    layui.use(
        [
            "layer",
            "form",
            "table",
            "admin",
            "upload",
            "tableTreeDj",
            "config",
            "laydate",
        ],
        function () {
            var $ = layui.jquery;
            var layer = layui.layer;
            var form = layui.form;
            var table = layui.table;
            var config = layui.config;
            var admin = layui.admin;
            var upload = layui.upload;
            var tableTree = layui.tableTreeDj;
            var laydate = layui.laydate;

            // 渲染表格
            var insTb = table.render({
                elem: "#warehouseOrderTable",
                url: config.base + "warehouse/warehouse/settled/list",
                toolbar: true,
                defaultToolbar: ["filter"],
                page: true,
                cellMinWidth: 100,
                cols: [
                    [
                        {type: "numbers", title: "序号", fixed: "left"},
                        {field: "settledOrder", title: "入驻订单号", align: "center"},
                        {field: "source", title: "数据来源", align: "center", templet: function (d) {
                                return d.source == 1 ? "平台" : "管理后台";
                            }},
                        {field: "customerName", title: "客户代码", align: "center"},
                        // {field: "customerLevelId", title: "客户级别", align: "center"},
                        {field: "factoryName", title: "申请入驻仓库", align: "center"},
                        {field: 'contactName', title: '联系人', align: 'center'},
                        {field: "contractCode", title: "合同编号", align: "center"},
                        {
                            field: "status",
                            title: "状态",
                            align: "center",
                            templet: function (d) {
                                switch (d.status) {
                                    case 1:
                                        return '<span style="color: #5FB878;">通过</span>'; // 绿色
                                    case 2:
                                        return '<span style="color: #999;">驳回</span>';
                                    case 3:
                                        return '<span style="color: #999;">已撤销</span>'; // 灰色
                                    default:
                                        return "未知状态";
                                }
                            },
                        },
                        {field: "createTime", title: "添加时间", align: "center"},
                        // ,{field: 'creator', title: '创建人' , align: 'center'}
                        {
                            align: "center",
                            toolbar: "#warehouseOrderTableBar",
                            title: "操作",
                            width: 200,
                            fixed: "right",
                        },
                    ],
                ],
                done: function (res, curr, count) {
                    console.log(res,"data")
                    //移除按钮
                    // var menuButton = config.getMenuButton(location.hash);
                    // if(menuButton.indexOf("搜索") == -1){
                    //     $('.layui-form.toolbar').remove();
                    // }
                    // if(menuButton.indexOf("修改") == -1){
                    //     $("[lay-event='edit']").remove();
                    // }
                    // if(menuButton.indexOf("删除") == -1){
                    //     $("[lay-event='del']").remove();
                    // }
                },
            });

            // 工具条点击事件
            table.on("tool(warehouseOrderTable)", function (obj) {
                var data = obj.data;
                if (obj.event === "info") {
                    //详情
                    admin.req(
                        "warehouse/warehouse/settled/info",
                        {id: data.id},
                        (res) => {
                            if (res.code === 200) {
                                let dataCont={...data,...res.data}
                                openFormFun("detail", dataCont);
                            }
                        }
                    );
                    // openFormFun("detail", data);
                    //处理详情数据进行包装
                } else if (obj.event === "process") {
                    console.log(data,"dayayay")
                    // admin.reqSync(
                    //     "enterprise/sell/admin/contactInfo/list?page=1&limit=9999",
                    //     {selCustomerId: data.warehouseCustomerId},
                    //     function (res) {
                    //         layer.closeAll("loading");
                    //         if (res.code == 200) {
                    //             res.data.map(item=>{
                    //                 if(item.name==data.contactName){
                    //                     data.telephone=item.telephone
                    //                     data.email=item.email
                    //                 }
                    //             })
                    //         }
                    //     },
                    //     "GET"
                    // );
                    // 审核
                    openFormFun("process", data);
                } else if (obj.event === "withdraw") {
                    // 撤回
                    updateStatus(data);
                }
            });
            /*详情页面*/
            function openFormFun(eventName, data) {
                admin.formOpen({
                    area: "90%",
                    offset: "5%",
                    title: eventName==='detail' ? "托管申请详情" : '申请审核',
                    path: "components/warehouse/product/settledForm.html",
                    success: function () {
                        data.type='托管合同'
                        parentData.eventName=eventName
                        parentData.data=[data]

                    }
                });
            }

            // 修改订单状态
            function updateStatus(data) {
                layer.confirm('确定撤销吗？', {
                    skin: 'layui-layer-admin'
                }, function (i) {
                    layer.close(i);
                    layer.load(2);
                    admin.req('warehouse/warehouse/settled/revoke', {id: data.id, status: 3}, function (data) {
                        layer.closeAll('loading');
                        if (data.code == 200) {
                            layer.msg(data.msg, {icon: 1});
                            table.reload('warehouseOrderTable');
                        } else {
                            layer.msg(data.msg, {icon: 2});
                        }
                    }, 'GET');
                });
            }
            //监听排序
            table.on("sort(warehouseOrderTable)", function (obj) {
                table.reload("warehouseOrderTable", {
                    initSort: obj,
                    where: {
                        sort: obj.field,
                        order: obj.type,
                    },
                });
            });
            // 搜索
            form.on("submit(formSubSearchLog)", function (data) {
                insTb.reload({where: data.field, page: {curr: 1}}, "data");
            });

            // 重置搜索
            form.on("submit(resets)", function (data) {
                insTb.reload(
                    {where: admin.resetSearch(data.field), page: {curr: 1}},
                    "data"
                );
            });
            // 添加按钮点击事件
            $("#warehouseOrderBtnAdd").click(function () {
                showEditModel();
            });
            // 显示编辑弹窗
            function showEditModel() {
                admin.formOpen({
                    type: 1,
                    area: '640px',
                    offset: '65px',
                    title: "添加托管订单",
                    content: $("#warehouseOrderContractForm").html(),
                    success: function () {
                        //客户信息
                        admin.reqSync(
                            "enterprise/sell/admin/customersInfo/list",
                            { limit: 9999 },
                            function (res) {
                                layer.closeAll("loading");
                                if (res.code == 200) {
                                    loadSelectHtml(res.data, "请选客户信息", "trusteeshipcontractId", 1);
                                }
                            },
                            "GET"
                        );

                        //客户信息下拉操作
                        form.on("select(trusteeshipcontractId)", function (data) {
                            if (data.value === "") {
                            } else {
                                let dataObj = JSON.parse(
                                    $(data.elem[data.elem.selectedIndex]).attr("data")
                                );
                                $("#customerId").val(dataObj.id);
                                userNameSelect(dataObj);
                            }
                        });

                        /*添加仓库编号*/
                        admin.reqSync(
                            "warehouse/warehouse/admin/administration/list",
                            {limit: 9999},
                            function (res) {
                                layer.closeAll("loading");
                                if (res.code == 200) {
                                    loadSelectHtml(res.data, "请选择仓库", "factoryId");
                                }
                            },
                            "GET"
                        );

                        function userNameSelect(data) {
                            admin.reqSync(
                                "enterprise/sell/admin/contactInfo/list?page=1&limit=9999",
                                {selCustomerId: data ? data.id : null},
                                function (res) {
                                    layer.closeAll("loading");
                                    if (res.code == 200) {
                                        loadSelectHtml(res.data, "请选客户信息", "contactId", 2);
                                    }
                                },
                                "GET"
                            );
                        }

                        form.on("select(contactId)", function (data) {
                            if (data.value === "") {
                            } else {
                                let dataObj = JSON.parse(
                                    $(data.elem[data.elem.selectedIndex]).attr("data")
                                );
                                $("#warehouseUserId").val(dataObj.id);
                                $("#phone").val(dataObj.telephone);
                                $("#mail").val(dataObj.email);
                            }
                        });
                        laydate.render({
                            elem: "#contractTime",
                            type: "datetime",
                        });
                        // 表单提交事件
                        form.on("submit(warehouseOrderContractFormSubmit)", function (d) {
                            const formData = {...d.field,source:2,customerLevelId:1};
                            layer.load(2);
                            admin.req("warehouse/warehouse/settled/add",JSON.stringify(formData),function (res) {
                                    layer.closeAll("loading");
                                    if (res.code == 200) {
                                        layer.msg(res.msg, {icon: 1});
                                        table.reload("warehouseOrderTable", {});
                                        layer.closeAll("page");
                                    } else {
                                        layer.msg(res.msg, {icon: 2});
                                    }
                                },"POST",
                                true,
                                "application/json; charset=utf-8"
                            );
                        });
                    },
                });
            }

            // option渲染
            function loadSelectHtml(data, tips, htmltarget, type) {
                let html = " <option value>" + tips + "</option>";
                if (!data || data == null) {
                    return;
                }
                for (let i = 0; i < data.length; i++) {
                    if (type == 1) {
                        html += `<option value='${data[i].id}' id='${
                            data[i].id
                        }' data='${JSON.stringify(data[i])}' >${
                            data[i].customerName
                        }</option>`;
                    } else if (type == 2) {
                        html += `<option value='${data[i].id}' id='${
                            data[i].id
                        }' data='${JSON.stringify(data[i])}'>${data[i].name}</option>`;
                    } else if (type == 3) {
                        html += `<option value='${data[i].id}' id='${data[i].id}'>${data[i].producerName}</option>`;
                    } else {
                        html += `<option value='${data[i].id}' data='${JSON.stringify(
                            data[i]
                        )}'>${data[i].name}</option>`;
                    }
                }

                $("form select[name='" + htmltarget + "']").html(html);
                form.render("select");
            }

        }
    );
</script>
